//
//  XQNetManager.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/23.
//

#import "XQNetManager.h"
#import "XQDevice.h"


@implementation XQNetManager

// 静态变量用于存储单例实例
static XQNetManager *sharedInstance = nil;

// 类方法实现
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init]; // 创建单例实例
    });
    return sharedInstance;
}

// 私有化初始化方法
- (instancetype)init {
    self = [super init];
    if (self) {
        NSString *continuous = [[NSUserDefaults standardUserDefaults] objectForKey:@"continuous"];
        self.globalDic = @{
            @"geosynchronous" : [XQDevice getAppVersion],
            @"miles" : [XQDevice getDeviceModel],
            @"therefore" : [XQDevice getIDFV],
            @"rather" : [XQDevice getIDFA],
            @"vehicle" : [XQDevice getSystemVersion],
        }.mutableCopy;
        if (continuous.length != 0) {
            [self.globalDic setValue:continuous forKey:@"continuous"];
        }
//        self.url = @"http://8.212.182.12:8670/stages";
        self.url = @"https://jpm.skinsciences-ph.com";
    }
    return self;
}

+ (BOOL)isLogin {
    NSString *continuous = [[NSUserDefaults standardUserDefaults] objectForKey:@"continuous"];
    return continuous.length;
}

+ (NSString *)getAbsolutePath:(NSString *)path {
    NSString *url = [NSString stringWithFormat:@"%@%@",[self sharedInstance].url,path];
    NSDictionary *dic = [self sharedInstance].globalDic;
    NSString *paramStr = @"";
    if (dic.count != 0) {
        NSMutableArray *params = [NSMutableArray array];
        for (NSString *key in dic.allKeys) {
            NSString *p = [NSString stringWithFormat:@"%@=%@",key,dic[key]];
            [params addObject:p];
        }
        paramStr = [params componentsJoinedByString:@"&"];
    }
    
    return [NSString stringWithFormat:@"%@?%@",url,paramStr];
}


+ (NSString *)getWebUrlAbsolutePath:(NSString *)path {
    NSDictionary *dic = [self sharedInstance].globalDic;
    NSString *paramStr = @"";
    if (dic.count != 0) {
        NSMutableArray *params = [NSMutableArray array];
        for (NSString *key in dic.allKeys) {
            NSString *p = [NSString stringWithFormat:@"%@=%@",key,dic[key]];
            [params addObject:p];
        }
        paramStr = [params componentsJoinedByString:@"&"];
    }
    
    if ([path containsString:@"?"]) {
        return [NSString stringWithFormat:@"%@&%@",path,paramStr];
    }else {
        return [NSString stringWithFormat:@"%@?%@",path,paramStr];
    }
    
}

@end
